import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: () => import('@/views/Home')
  },
  {
    path: '/register',
    name: 'register',
    component: () => import('@/views/auth/Register'),
    meta: { title: '注册' }
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/auth/Login'),
    meta: { title: '登录' }
  },
  // 加上requireAuth: true 必须登录才能跳转
  {
    path: '/post/create',
    name: 'post-create',
    component: () => import('@/views/post/Create'),
    meta: { title: '帖子发布', requireAuth: true }
  },
  {
    name: 'topic-edit',
    path: '/topic/edit/:id',
    component: () => import('@/views/post/Edit'),
    meta: { title: '编辑', requireAuth: true }
  },
  {
    path: '/post/:id',
    name: 'post-detail',
    component: () => import('@/views/post/Detail'),
    meta: { title: '帖子详情' }
  },
  {
    name: 'tag',
    path: '/tag/:name',
    component: () => import('@/views/tag/Tag'),
    meta: { title: '主题列表' }
  },
  {
    name: 'search',
    path: '/search',
    component: () => import('@/views/Search'),
    meta: { title: '检索' }
  },
  {
    name: 'user',
    path: '/member/:username/home',
    component: () => import('@/views/user/Profile'),
    meta: { title: '用户主页' }
  },
  {
    name: 'user-setting',
    path: '/member/:username/setting',
    component: () => import('@/views/user/Setting'),
    meta: { title: '个人设置', requireAuth: true }
  },
  {
    path: '/404',
    name: '404',
    component: () => import('@/views/error/404'),
    meta: { title: '404-Notfound' }
  },
  // 输入以上其他页面 重定向404
  {
    path: '*',
    redirect: '/404',
    hidden: true
  }
]

// 捕获在首页点击退出时报的异常
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err)
}

const router = new VueRouter({
  routes
})

export default router
